<?php
namespace app\sendmsg\model;
use app\admin\model\CardUser;
use app\BaseModel;
use app\shop\model\IndexUserInfo;

class CardTimeline extends BaseModel{
//定义表名
    protected $name = 'longbing_card_timeline';
    public function dtlist($page,$list_rows,$where = [],$staff_id=0,$userId=0){

        $data = $this->alias( 'a' )->where($where)
           ->field( [ 'a.id', 'a.title', 'a.cover', 'a.top', 'a.create_time', 'a.release_time', 'a.user_id', 'a.type', 'a.thumb','a.url_type','a.content','article_id','a.status','a.uniacid', 'b.name as nickName','b.avatar as avatarUrl' ])
           ->join( 'longbing_card_user_info b', 'a.user_id = b.fans_id', 'LEFT' )
            ->order('a.top desc,a.id desc')
           ->paginate([ 'list_rows' =>$list_rows, 'page' => $page ])->each(function($item, $key)use ($staff_id,$userId){
                if(!empty($item['content'])){
                    $item['content'] = htmlspecialchars_decode($item['content']);
                }
               if($item['user_id']==0&&$staff_id){
                   $card_company = new CardCompany();
                   $userinfo = new IndexUserInfo();
                   $company_id  = $userinfo->getCompanyId(['fans_id'=>$staff_id]);
                   //找到公司最上级
                   $company_id = $card_company->getUserTopCompanyId($company_id);
                   $company = $card_company->getinfo(['uniacid'=>$item['uniacid'],'id'=>$company_id]);
                   $item['nickName'] = $company['name'];
                   $item['avatarUrl'] = $company['logo'];
               }
                if(!$item['nickName']){
                    if(!$staff_id){
                        $item['nickName'] = '公司发布';
                    }else{
                        $item['nickName'] = '--';
                    }
                }
               //查询点赞人数
               $thumds = new CardTimelineThumbs();
               $item['is_fabulous'] = $thumds->is_fabulous($userId,$item['id'],$item['uniacid'])==null?0:1;
               $comment = new CardTimelineComment();
               $item['user'] = $thumds->getUserFabulous($item['id'],5);
               $item['comment'] = $comment->getLineComment(['a.timeline_id'=>$item['id']],1,2);
                if($item['type'] == 1){
                    if(strstr($item['content'], '/v.qq.com/')){
                        $item['vid'] = getStr($item['content']);
                    }else{
                        $item['content'] = transImagesOne($item->toArray(), ['content'])['content'];
                    }
                }
                if($item['thumb'] == 0){
                    $item['thumb'] = $thumds->fabulousCount(['timeline_id'=>$item['id'],'status'=>1,'uniacid'=>$item['uniacid']]);
                }
               return $item;
           })
           ->toArray();
        $data['data'] = transImages($data['data'], ['cover']);
        $data['data'] = transImagesOne($data['data'], ['avatarUrl']);
        $data['data'] = lbHandelRadarDate($data['data'],'create_time');
        return $data;
    }
    public function get_info($where=[],$params,$page,$limit){
        $data = $this->alias( 'a' )
            ->field( [ 'a.id', 'a.title', 'a.cover', 'a.top', 'a.create_time', 'a.user_id', 'a.type', 'a.thumb','a.url_type','a.content','article_id','a.status','a.uniacid', 'b.name as nickName','b.avatar as avatarUrl' ])
            ->join( 'longbing_card_user_info b', 'a.user_id = b.fans_id', 'LEFT' )
            ->where($where)
            ->find()->toArray();
        if($data){
            $thumds = new CardTimelineThumbs();
            if($data['thumb'] == 0){
                $data['thumb'] = $thumds->fabulousCount(['timeline_id'=>$data['id'],'status'=>1,'uniacid'=>$data['uniacid']]);
            }
            if($data['user_id']==0){
                $card_company = new CardCompany();
                $userinfo = new IndexUserInfo();
                $company_id  = $userinfo->getCompanyId(['fans_id'=>$params['staff_id']]);
                //找到公司最上级
                $company_id = $card_company->getUserTopCompanyId($company_id);
                $company = $card_company->getinfo(['uniacid'=>$data['uniacid'],'id'=>$company_id]);
                $data['nickName'] = $company['name'];
                $data['avatarUrl'] = $company['logo'];
            }
            if(!$data['nickName']){
                $data['nickName'] = '--';
            }

            $data['content'] = htmlspecialchars_decode($data['content']);
            $data['is_fabulous'] = $thumds->is_fabulous($params['user_id'],$data['id'],$data['uniacid'])==null?0:1;
            $data['user'] = $thumds->getUserFabulous($params['id'],5);
            $comment = new CardTimelineComment();
            $data['comment'] = $comment->getLineComment(['a.timeline_id'=>$data['id']],$page,$limit);
            if($data['type'] ==1){
                if(strstr($data['content'], '/v.qq.com/')){
                    $data['vid'] = getStr($data['content']);
                }else{
                    $data['content'] = transImagesOne($data, ['content'])['content'];
                }
            }
        }
        $data = transImages($data, ['cover']);
        $data = transImagesOne($data, ['avatarUrl']);
        $data = datachange($data);
        return $data;
    }
    public function admin_get_info($where){
        $data = $this->where($where)->find()->toArray();
        if($data){
            if(!empty($data['content'])){
                $data['content'] = htmlspecialchars_decode($data['content']);
            }
        }
        return $data = transImages($data, ['cover']);
    }
    public function getID($id){
        return $this->where('id',$id)->value('id');
    }
    public function getUserid($id){
       return $this->where('id',$id)->value('user_id');
    }
	//修改动态
	public function updateTimeline($filter ,$data)
	{
		$data['update_time'] = time();
		$result = $this->where($filter)->update($data);
		return $result;
	}
	//动态数量
    public function getCount($where){
        return $this->where($where)->count();
    }
}